package com.houtai.service.dict;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzDict;
import com.houtai.bean.dto.dict.TsJzDictListPage;
import com.houtai.bean.dto.dict.TsJzDictParentListPage;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.TsJzDictDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DictService {
    private static Logger logger = Logger.getLogger(DictService.class);
    @Autowired
    private TsJzDictDao tsJzDictDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找数据列表
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findDictByParentId(TsJzDictParentListPage page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findDictByParentId:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "dict_sort DESC, dict_id asc");
            List<TsJzDict> lists = tsJzDictDao.findDictByParentId(page.getParent_id());
            PageInfo<TsJzDict> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("parent_id", page.getParent_id());
            map.put("code_type", "city");
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找数据列表 父ID：" + page.getParent_id() , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   查找数据列表
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findDictList(TsJzDictListPage page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findDictList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "dict_sort DESC, dict_id asc");
            List<TsJzDict> lists = tsJzDictDao.findDictByType(page.getCode_type());
            PageInfo<TsJzDict> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("code_type", page.getCode_type());
            map.put("parent_id", 0);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找数据列表 数据类型：" + page.getCode_type() , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   删除数据
     * @param     dict_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo deleteDict(int dict_id , HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertRole:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tsJzDictDao.updateDictStatus(dict_id);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"删除数据 数据id:" + dict_id , request);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
            throw new RuntimeException();
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   添加数据
     * @param     tsJzDict
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo insertTsJzDict(TsJzDict tsJzDict, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTsJzDict:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tsJzDictDao.insertTsJzDict(tsJzDict);
            retInfo.setMark("0");
            retInfo.setTip("创建成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加数据,id:" + tsJzDict.getDict_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改数据
     * @param     tsJzDict
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTsJzDict(TsJzDict tsJzDict, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTsJzDict:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tsJzDictDao.updateTsJzDict(tsJzDict);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改数据,id:" + tsJzDict.getDict_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public List<TsJzDict> findAllDictByParentId(int parent_id) {
        return tsJzDictDao.findDictByParentId(parent_id);
    }
    public TsJzDict findTsJzDictById(Integer dict_id) {
        return tsJzDictDao.findDictById(dict_id);
    }
    public List<TsJzDict> findTsJzDict(String code_type) {
        return tsJzDictDao.findDictByType(code_type);
    }
}
